package messagesvc.websocket.config;

import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * SDK 自带的方式
 */
@Slf4j
//@Configuration
//@EnableWebSocket
//@ServerEndpoint("/ws/txt")
//@Component
public class WebSocketServer {


  @Bean
  public ServerEndpointExporter serverEndpointExporter() {
    return new ServerEndpointExporter();
  }

  /**
   * 连接成功
   */
  @OnOpen
  public void onOpen(Session session) {
    System.out.println("连接成功");
  }

  /**
   * 连接关闭
   */
  @OnClose
  public void onClose(Session session) {
    System.out.println("连接关闭");
  }

  /**
   * 接收到消息
   */
  @OnMessage
  public String onMsg(String text) throws IOException {
    return "servet 发送：" + text;
  }

}
